package com.ebt.m.presenter;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.PolicyEntity;
import com.ebt.m.commons.buscomponent.listview.RecyclerFootManger;
import com.ebt.m.utils.ConfigData;
import com.ebt.m.commons.buscomponent.listview.SectionItem;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by andy.wang on 2016/12/14.
 */

public class MessionPresenter extends BaseListPresenter<BaseListContract.IView> {
    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }

    public static final int TYPE_UnReceiptBack = 0;
    public static final int TYPE_RECEIVABLE = 1;

    private int position;
    private int currentPage0 = 1;
    private int currentPage1 = 1;

    public MessionPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        currentPage0 = 1;
        currentPage1 = 1;
        position = (int) args[0];
        EBTAPI erpApi = AppInitService.getEbtApi();
        if (position == TYPE_UnReceiptBack) {
            erpApi.getUnReceiptBacks(currentPage0, ConfigData.PAGE_COUNT_LIMIT)
                    .filter(getFilterForFootviewStatus(true))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        iView.updateData(createSectionItem(response.getData(), position));
                        iView.updateCount(response.getPaging());
                    }, throwable -> {
                        throwable.printStackTrace();
                        iView.errorData();
                    });
        } else {
            erpApi.getReceivablePolicy(currentPage1, ConfigData.PAGE_COUNT_LIMIT)
                    .filter(getFilterForFootviewStatus(true))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        iView.updateData(createSectionItem(response.getData(), position));
                        iView.updateCount(response.getPaging());
                    }, throwable -> {
                        throwable.printStackTrace();
                        iView.errorData();
                    });
        }

    }

    @Override
    public void refresh() {
        loadNew(position);
    }

    @Override
    public void loadNext(Object... args) {
        position = (int) args[0];
        EBTAPI erpApi = AppInitService.getEbtApi();
        if (position == TYPE_UnReceiptBack) {
            erpApi.getUnReceiptBacks(currentPage0+1, ConfigData.PAGE_COUNT_LIMIT)
                    .filter(getFilterForFootviewStatus(false))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        currentPage0++;
                        iView.updateDataAdd(createSectionItem(response.getData(), position));
                    }, throwable -> {
                        throwable.printStackTrace();
                        iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
                    });
        } else {
            erpApi.getReceivablePolicy(currentPage1+1, ConfigData.PAGE_COUNT_LIMIT)
                    .filter(getFilterForFootviewStatus(false))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(response -> {
                        currentPage1++;
                        iView.updateDataAdd(createSectionItem(response.getData(), position));
                    }, throwable -> {
                        throwable.printStackTrace();
                        iView.setFootStatus(RecyclerFootManger.STATUS_ERROR, true);
                    });
        }
    }

    public List<SectionItem> createSectionItem(List list, int position) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (list == null || list.size() < 1) {
            return sections;
        }
        try {
            for (int i = 0; i < list.size(); i++) {
                PolicyEntity policyEntity = (PolicyEntity) list.get(i);
                SectionItem item = new SectionItem(position, policyEntity);
                sections.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }
}
